﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Common;
using System.Configuration;
using System.IO;

namespace Workflow.Web
{
    public partial class ViewTiffActiveX : System.Web.UI.Page
    {


        public string FilePath;
        public int _cnt = 0;
        private string pri_TifBasePath="";
        protected void Page_Load(object sender, EventArgs e)
        {
            FilePath = Server.UrlDecode(Request.QueryString["File"].ToString());
            pri_TifBasePath = FilePath;
            if (FilePath.Contains("／"))
            {
                FilePath = FilePath.Replace("／", Server.UrlEncode("／"));
            }

            //Ext Files
            String fileName = FilePath.Substring(FilePath.LastIndexOf("\\") + 1);
            fileName = fileName.Replace(".tif", "");

            if (Request.QueryString["_ext_cnt"] == null)
            {
                _cnt = this.checkExt();

            }
            else
            { 
                _cnt = Convert.ToInt32(Request.QueryString["_ext_cnt"].ToString());

                int file_cnt = Convert.ToInt32(Request.QueryString["_ext_file"].ToString());
                if (file_cnt>0)
                {
                    FilePath = FilePath.Replace(".tif", "-" + file_cnt + ".tif"); 
                }
            }

            String _sExtCnt = "&_ext_cnt=";
            String _sExtFile = "&_ext_file=";

            String _url = Request.Url.ToString();
            if (_url.Contains("&_ext_cnt"))
            {
                _url = _url.Substring(0, _url.IndexOf("&_ext_cnt"));
            }
            for (int i = 0; i <= _cnt; i++)
            {
                HyperLink _hl = new HyperLink();
                _hl.Text = fileName +( i>0?"-"+i:"")+ "<br/>";

                if (_url.Contains("R&D"))
                {
                    _url = _url.Replace("R&D", Server.UrlEncode("R&D"));
                }
                _hl.NavigateUrl = _url + _sExtCnt + _cnt + _sExtFile + i;
                this._ph_Ext.Controls.Add(_hl);
            }


            FilePath = "\"" + FilePath + "\"";
        }


        private int checkExt(){
            int _ext = 0;

            String _path = "";

            if(pri_TifBasePath.Contains(ConfigurationManager.AppSettings["TifBasePath_View"]))
                _path = pri_TifBasePath.Replace(ConfigurationManager.AppSettings["TifBasePath_View"], ConfigurationManager.AppSettings["TifBasePath"]);


            if (pri_TifBasePath.Contains(ConfigurationManager.AppSettings["TifBasePath_ViewMDM"]))
                _path = pri_TifBasePath.Replace(ConfigurationManager.AppSettings["TifBasePath_ViewMDM"], ConfigurationManager.AppSettings["TifBasePathMDM"]);

            if (pri_TifBasePath.Contains(ConfigurationManager.AppSettings["TifBasePath_ViewAHPTP"]))
                _path = pri_TifBasePath.Replace(ConfigurationManager.AppSettings["TifBasePath_ViewAHPTP"], ConfigurationManager.AppSettings["TifBasePathAHPTP"]);


            if (pri_TifBasePath.Contains(ConfigurationManager.AppSettings["TifBasePath_ViewAHEEM"]))
                _path = pri_TifBasePath.Replace(ConfigurationManager.AppSettings["TifBasePath_ViewAHEEM"], ConfigurationManager.AppSettings["TifBasePathAHEEM"]);




            //if (ImpersonateLogon.impersonateValidUser(ConfigurationManager.AppSettings["ImpersonateUser"],
            //                         ConfigurationManager.AppSettings["ImpersonateDomain"],
            //                         ConfigurationManager.AppSettings["ImpersonatePassword"]))
            //{


                for (; ; _ext++)
                {
                    if (!File.Exists(_path.Replace(".tif", "-" + (_ext + 1) + ".tif")))
                        break;
                }
               

            //    ImpersonateLogon.undoImpersonation();
            //}
            //else
            //{
            //    throw new Exception("Impersonate Logon Error!");
            //}

            return _ext;
        }
    }
}